<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./lib/vue.min.js"></script>
    <script src="./lib/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <router-link to = "/login">登录</router-link>
        <router-link to = "/register">注册</router-link>

        <router-view></router-view>
    </div>
    
    <script>
        var login = {
            template: '<h3>这是登录子组件</h3>'
        }

        var register = {
            template: '<h3>这是注册子组件</h3>'
        }
        
        const router = new VueRouter({
            routes: [
                { path: '/', redirect: '/login'},
                { path: '/login', component: login},
                { path: '/register', component: register}
            ]
        })

        var vm = new Vue({
            el:"#app",
            data:{},
            methods: {},
            //将路由绑定到vue实例中 router:router
            router,
            watch:{
                //this.$route.path
                '$route.path': function(newVal, oldVal) {
                    console.log("当前页面是" + newVal + "--- " + "旧页面是:" + oldVal)
                    if(newVal == '/login') {
                        alert('欢迎来到登录页面!')
                    }else if(newVal == "/register") {
                        alert('欢迎来到注册页面!')
                    }
                }
            }
        })
    </script>
</body>
</html>